#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    socket = new QTcpSocket(this);

    socket->connectToHost("127.0.0.1",8081);

    connect(socket,&QTcpSocket::readyRead,this,[=]()->void{
        QString response = socket->readAll();

        ui->listWidget->addItem("服务器："+response);
    });
    bool ret = socket->waitForConnected();
    if(!ret)
    {
        QMessageBox::critical(this,"链接失败",socket->errorString());
        exit(1);
    }
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    const QString& text = ui->lineEdit->text();
    socket->write(text.toUtf8());

    ui->listWidget->addItem("客户端："+text);
    ui->lineEdit->clear();
}
